package com.lxy.testsurfaceview;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CameraActivity extends Activity {
	private SurfaceView surfaceView;
	private Button bu;
	private static final String TAG="hello";
	
	private Camera camera;   
    private boolean preview  = false ; 
    private byte bb[];
    
    private Handler han=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if(msg.what==1){
				Intent intent=new Intent();
				Bundle b=new Bundle();
				b.putByteArray("photo", bb);
				intent.putExtra("photo", b);
				setResult(2,intent);
				finish();
			}
		}
    };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.camera_layout);
		surfaceView=(SurfaceView) findViewById(R.id.surfaceView);
		bu=(Button) findViewById(R.id.button);
		
	    SurfaceHolder holder = surfaceView.getHolder();
        holder.setFixedSize(200, 200);  
        holder.setKeepScreenOn(true);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.addCallback(new MySurfaceCallback());
		
		bu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				takepicture(v);
			}
		});
	}
	
	private final class MySurfaceCallback implements Callback{
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
        }
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                // 当surfaceview创建就去打开相机
                camera = Camera.open();
                camera.setDisplayOrientation(90);//旋转度数
                Parameters params = camera.getParameters();
                Log.i("i", params.flatten());
                params.setJpegQuality(80);  // 设置照片的质量
                params.setPictureSize(1024, 768);
                params.setPreviewFrameRate(5);  // 预览帧率
                camera.setParameters(params); // 将参数设置给相机
                // 设置预览显示
                camera.setPreviewDisplay(surfaceView.getHolder());
                // 开启预览
                camera.startPreview();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if(camera != null){
                camera.release();
                camera = null;
            }
        }
        
    }

    public void takepicture(View v){
        camera.takePicture(null, null, new MyPictureCallback());
    }
    
    private final class MyPictureCallback implements PictureCallback{
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
	        bb=data;
	        camera.stopPreview();
	        han.sendEmptyMessage(1);
        }
    }
}
